<?php
require_once 'Zend/View/Interface.php';
require_once 'Smarty/Smarty.class.php';
require_once 'application/library/helper/configHelper.php';

/** 
 * smarty操作用类
 * gcViewSmarty
 * @author    Soochow University
 * @version   1.0
 * @exclude   作  成  日：2011/04/07
 */
class gcViewSmarty implements Zend_View_Interface{
    
    /**
     *smarty
     */
    protected $_smarty;
    
    /**
     * 类构造函数
     * @param string $tmplPath 
     * @param string $extraParams
     * @return 
     */
    public function __construct($tmplPath = null, $extraParams = array()){
         $this->_smarty = new Smarty;
         if (null !== $tmplPath) {
              $this->setScriptPath($tmplPath);
         }

         foreach ($extraParams as $key => $value) {
             $this->_smarty->$key = $value;
         }
    }
    
    /**
     * @see Zend/View/Zend_View_Interface::getEngine()
     */
    public function getEngine(){
        return $this->_smarty;
    }

    /**
     * @see Zend/View/Zend_View_Interface::setScriptPath()
     */
    public function setScriptPath($path){
        if (is_readable($path)){
            $this->_smarty->template_dir    = $path.configHelper::getSmartyTemplateDir();
            if( @count(@explode("/",configHelper::getSmartyCompileDir()))>2){
                $this->_smarty->compile_dir     = configHelper::getSmartyCompileDir();
            }else {
                $this->_smarty->compile_dir     = $path.configHelper::getSmartyCompileDir();
            }
            $this->_smarty->left_delimiter  = "{[";
            $this->_smarty->right_delimiter = "]}";
            return;
        }
    }
    
    public function setTemplatePath($path) {
        $this->_smarty->template_dir = $path;
    }

    /**
     * @see Zend/View/Zend_View_Interface::getScriptPaths()
     */
    public function getScriptPaths(){
        return array($this->_smarty->template_dir);
    }

    /**
     * @see Zend/View/Zend_View_Interface::setBasePath()
     */
    public function setBasePath($path, $classPrefix = 'Zend_View'){
        return $this->setScriptPath($path);
    }

    /**
     * @see Zend/View/Zend_View_Interface::addBasePath()
     */
    public function addBasePath($path, $classPrefix = 'Zend_View'){
        return $this->setScriptPath($path);
    }

    /**
     * @see Zend/View/Zend_View_Interface::__set()
     */
    public function __set($key, $val){
        $this->_smarty->assign($key, $val);
    }

    /**
     * @see Zend/View/Zend_View_Interface::__isset()
     */
    public function __isset($key){
        return (null !== $this->_smarty->get_template_vars($key));
    }

    /**
     * @see Zend/View/Zend_View_Interface::__unset()
     */
    public function __unset($key){
        $this->_smarty->clear_assign($key);
    }

    /**
     * @see Zend/View/Zend_View_Interface::assign()
     */
    public function assign($spec, $value = null){
         if (is_array($spec)) {
             $this->_smarty->assign($spec);
             return;
         }
         $this->_smarty->assign($spec, $value);
    }

    /**
     * @see Zend/View/Zend_View_Interface::clearVars()
     */
    public function clearVars(){
        $this->_smarty->clear_all_assign();
    }

    /**
     * @see Zend/View/Zend_View_Interface::render()
     */
    public function render($name){
        return $this->_smarty->display($name);
    }
    
    /**
     * 取得生成HTML
     * fetch
     * @param unknown_type $name
     */
    public function fetch($name){
        return $this->_smarty->fetch($name);
    }
}

